﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using SPL.Entities;
using SPL.Data;

#endregion

namespace SPL.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="KvtProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class KvtProviderBaseCore : EntityProviderBase<SPL.Entities.Kvt, SPL.Entities.KvtKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, SPL.Entities.KvtKey key)
		{
			return Delete(transactionManager, key.Id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int64 id)
		{
			return Delete(null, id);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int64 id);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_Status key.
		///		FK_KVT_Status Description: 
		/// </summary>
		/// <param name="status"></param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByStatus(System.Int32 status)
		{
			int count = -1;
			return GetByStatus(status, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_Status key.
		///		FK_KVT_Status Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="status"></param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		/// <remarks></remarks>
		public SPL.Entities.TList<Kvt> GetByStatus(TransactionManager transactionManager, System.Int32 status)
		{
			int count = -1;
			return GetByStatus(transactionManager, status, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_Status key.
		///		FK_KVT_Status Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="status"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByStatus(TransactionManager transactionManager, System.Int32 status, int start, int pageLength)
		{
			int count = -1;
			return GetByStatus(transactionManager, status, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_Status key.
		///		fkKvtStatus Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="status"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByStatus(System.Int32 status, int start, int pageLength)
		{
			int count =  -1;
			return GetByStatus(null, status, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_Status key.
		///		fkKvtStatus Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="status"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByStatus(System.Int32 status, int start, int pageLength,out int count)
		{
			return GetByStatus(null, status, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_Status key.
		///		FK_KVT_Status Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="status"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public abstract SPL.Entities.TList<Kvt> GetByStatus(TransactionManager transactionManager, System.Int32 status, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_User key.
		///		FK_KVT_User Description: 
		/// </summary>
		/// <param name="inputUser"></param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByInputUser(System.Int32 inputUser)
		{
			int count = -1;
			return GetByInputUser(inputUser, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_User key.
		///		FK_KVT_User Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="inputUser"></param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		/// <remarks></remarks>
		public SPL.Entities.TList<Kvt> GetByInputUser(TransactionManager transactionManager, System.Int32 inputUser)
		{
			int count = -1;
			return GetByInputUser(transactionManager, inputUser, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_User key.
		///		FK_KVT_User Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="inputUser"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByInputUser(TransactionManager transactionManager, System.Int32 inputUser, int start, int pageLength)
		{
			int count = -1;
			return GetByInputUser(transactionManager, inputUser, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_User key.
		///		fkKvtUser Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="inputUser"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByInputUser(System.Int32 inputUser, int start, int pageLength)
		{
			int count =  -1;
			return GetByInputUser(null, inputUser, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_User key.
		///		fkKvtUser Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="inputUser"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByInputUser(System.Int32 inputUser, int start, int pageLength,out int count)
		{
			return GetByInputUser(null, inputUser, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_User key.
		///		FK_KVT_User Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="inputUser"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public abstract SPL.Entities.TList<Kvt> GetByInputUser(TransactionManager transactionManager, System.Int32 inputUser, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_KVT key.
		///		FK_KVT_KVT Description: 
		/// </summary>
		/// <param name="hid"></param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByHid(System.Int64? hid)
		{
			int count = -1;
			return GetByHid(hid, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_KVT key.
		///		FK_KVT_KVT Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="hid"></param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		/// <remarks></remarks>
		public SPL.Entities.TList<Kvt> GetByHid(TransactionManager transactionManager, System.Int64? hid)
		{
			int count = -1;
			return GetByHid(transactionManager, hid, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_KVT key.
		///		FK_KVT_KVT Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="hid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByHid(TransactionManager transactionManager, System.Int64? hid, int start, int pageLength)
		{
			int count = -1;
			return GetByHid(transactionManager, hid, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_KVT key.
		///		fkKvtKvt Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="hid"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByHid(System.Int64? hid, int start, int pageLength)
		{
			int count =  -1;
			return GetByHid(null, hid, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_KVT key.
		///		fkKvtKvt Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="hid"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public SPL.Entities.TList<Kvt> GetByHid(System.Int64? hid, int start, int pageLength,out int count)
		{
			return GetByHid(null, hid, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_KVT_KVT key.
		///		FK_KVT_KVT Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="hid"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of SPL.Entities.Kvt objects.</returns>
		public abstract SPL.Entities.TList<Kvt> GetByHid(TransactionManager transactionManager, System.Int64? hid, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override SPL.Entities.Kvt Get(TransactionManager transactionManager, SPL.Entities.KvtKey key, int start, int pageLength)
		{
			return GetById(transactionManager, key.Id, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_KVT index.
		/// </summary>
		/// <param name="id"></param>
		/// <returns>Returns an instance of the <see cref="SPL.Entities.Kvt"/> class.</returns>
		public SPL.Entities.Kvt GetById(System.Int64 id)
		{
			int count = -1;
			return GetById(null,id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KVT index.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SPL.Entities.Kvt"/> class.</returns>
		public SPL.Entities.Kvt GetById(System.Int64 id, int start, int pageLength)
		{
			int count = -1;
			return GetById(null, id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KVT index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SPL.Entities.Kvt"/> class.</returns>
		public SPL.Entities.Kvt GetById(TransactionManager transactionManager, System.Int64 id)
		{
			int count = -1;
			return GetById(transactionManager, id, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KVT index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SPL.Entities.Kvt"/> class.</returns>
		public SPL.Entities.Kvt GetById(TransactionManager transactionManager, System.Int64 id, int start, int pageLength)
		{
			int count = -1;
			return GetById(transactionManager, id, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KVT index.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="SPL.Entities.Kvt"/> class.</returns>
		public SPL.Entities.Kvt GetById(System.Int64 id, int start, int pageLength, out int count)
		{
			return GetById(null, id, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_KVT index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="id"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="SPL.Entities.Kvt"/> class.</returns>
		public abstract SPL.Entities.Kvt GetById(TransactionManager transactionManager, System.Int64 id, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a SPL.Entities.TList&lt;Kvt&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="SPL.Entities.TList&lt;Kvt&gt;"/></returns>
		public static SPL.Entities.TList<Kvt> Fill(IDataReader reader, SPL.Entities.TList<Kvt> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			SPL.Entities.Kvt c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("Kvt")
			.Append("|").Append((reader.IsDBNull(((int)SPL.Entities.KvtColumn.Id - 1))?(long)0:(System.Int64)reader[((int)SPL.Entities.KvtColumn.Id - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<Kvt>(
			key.ToString(), // EntityTrackingKey
			"Kvt",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new SPL.Entities.Kvt();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.Id = (System.Int64)reader["Id"];
			c.I1InputDate = (System.DateTime)reader["I1InputDate"];
			c.I9KhiVaoDoGppNhap = (System.Double)reader["I9KhiVaoDoGPPNhap"];
			c.I21XeBonFiqBupro = (System.Double)reader["I21XeBonFIQBupro"];
			c.I34NhapCondBh = (System.Double)reader["I34NhapCondBH"];
			c.I35MeBhSoKctv = (System.Double)reader["I35MeBHSoKCTV"];
			c.I36NhapCondBhTuTk125 = (System.Double)reader["I36NhapCondBHTuTK125"];
			c.I37NhapLpgTuTk107 = (System.Double)reader["I37NhapLPGTuTK107"];
			c.I38Fqic1011 = (System.Double)reader["I38FQIC1011"];
			c.I39Fqic1012 = (System.Double)reader["I39FQIC1012"];
			c.I41XuatCondBhTheoMe = (System.Double)reader["I41XuatCondBHTheoMe"];
			c.I42XuatCondBhLenhXuatSo = (System.String)reader["I42XuatCondBHLenhXuatSo"];
			c.I43MeTdXuatBupro = (System.Double)reader["I43METdXuatBupro"];
			c.I44TankXuatBupro = (System.Double)reader["I44TankXuatBupro"];
			c.I45GiamDinhXuatBupro = (System.Double)reader["I45GiamDinhXuatBupro"];
			c.I46XuatKhacLpg = (System.Double)reader["I46XuatKhacLPG"];
			c.I47XuatKhacCondBh = (System.Double)reader["I47XuatKhacCondBH"];
			c.I48XaDotLpg = (System.Double)reader["I48XaDotLPG"];
			c.I49XaDotCondBh = (System.Double)reader["I49XaDotCondBH"];
			c.I52XaDotKahxntlpg = (System.Double)reader["I52XaDotKAHXNTLPG"];
			c.I53XaDotKahxntCond = (System.Double)reader["I53XaDotKAHXNTCond"];
			c.I54XuatCondBhTank = (System.Double)reader["I54XuatCondBHTank"];
			c.I55XuatCondBhGiamDinh = (System.Double)reader["I55XuatCondBHGiamDinh"];
			c.I56TonCondBh = (System.Double)reader["I56TonCondBH"];
			c.I57TonBupro = (System.Double)reader["I57TonBupro"];
			c.I58TenMeMncs = (System.String)reader["I58TenMeMNCS"];
			c.I59VaoDoz3002 = (System.Double)reader["I59VaoDOZ3002"];
			c.I60RaDoz7900 = (System.Double)reader["I60RaDOZ7900"];
			c.I61MeMncsSoLift = (System.String)reader["I61MeMNCSSoLift"];
			c.I62XuatMeMncs = (System.Double)reader["I62XuatMeMNCS"];
			c.I63TonCondNcsTaiKctv = (System.Double)reader["I63TonCondNCSTaiKCTV"];
			c.I64XuatCondNcsTheoTank = (System.Double)reader["I64XuatCondNCSTheoTank"];
			c.I65XuatCondNcsTheoGiamDinh = (System.Double)reader["I65XuatCondNCSTheoGiamDinh"];
			c.I66SuKienVanHanh = (System.String)reader["I66SuKienVanHanh"];
			c.I67NoteBcn = (System.String)reader["I67NoteBCN"];
			c.I68NoteQlsplbh = (System.String)reader["I68NoteQLSPLBH"];
			c.I69NoteCondBhMe = (System.String)reader["I69NoteCondBHMe"];
			c.I70NoteGdme = (System.String)reader["I70NoteGDME"];
			c.I71NoteMncs = (System.String)reader["I71NoteMNCS"];
			c.Status = (System.Int32)reader["Status"];
			c.InputUser = (System.Int32)reader["InputUser"];
			c.Hid = reader.IsDBNull(reader.GetOrdinal("Hid")) ? null : (System.Int64?)reader["Hid"];
			c.CreatedDate = (System.DateTime)reader["CreatedDate"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="SPL.Entities.Kvt"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="SPL.Entities.Kvt"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, SPL.Entities.Kvt entity)
		{
			if (!reader.Read()) return;
			
			entity.Id = (System.Int64)reader["Id"];
			entity.I1InputDate = (System.DateTime)reader["I1InputDate"];
			entity.I9KhiVaoDoGppNhap = (System.Double)reader["I9KhiVaoDoGPPNhap"];
			entity.I21XeBonFiqBupro = (System.Double)reader["I21XeBonFIQBupro"];
			entity.I34NhapCondBh = (System.Double)reader["I34NhapCondBH"];
			entity.I35MeBhSoKctv = (System.Double)reader["I35MeBHSoKCTV"];
			entity.I36NhapCondBhTuTk125 = (System.Double)reader["I36NhapCondBHTuTK125"];
			entity.I37NhapLpgTuTk107 = (System.Double)reader["I37NhapLPGTuTK107"];
			entity.I38Fqic1011 = (System.Double)reader["I38FQIC1011"];
			entity.I39Fqic1012 = (System.Double)reader["I39FQIC1012"];
			entity.I41XuatCondBhTheoMe = (System.Double)reader["I41XuatCondBHTheoMe"];
			entity.I42XuatCondBhLenhXuatSo = (System.String)reader["I42XuatCondBHLenhXuatSo"];
			entity.I43MeTdXuatBupro = (System.Double)reader["I43METdXuatBupro"];
			entity.I44TankXuatBupro = (System.Double)reader["I44TankXuatBupro"];
			entity.I45GiamDinhXuatBupro = (System.Double)reader["I45GiamDinhXuatBupro"];
			entity.I46XuatKhacLpg = (System.Double)reader["I46XuatKhacLPG"];
			entity.I47XuatKhacCondBh = (System.Double)reader["I47XuatKhacCondBH"];
			entity.I48XaDotLpg = (System.Double)reader["I48XaDotLPG"];
			entity.I49XaDotCondBh = (System.Double)reader["I49XaDotCondBH"];
			entity.I52XaDotKahxntlpg = (System.Double)reader["I52XaDotKAHXNTLPG"];
			entity.I53XaDotKahxntCond = (System.Double)reader["I53XaDotKAHXNTCond"];
			entity.I54XuatCondBhTank = (System.Double)reader["I54XuatCondBHTank"];
			entity.I55XuatCondBhGiamDinh = (System.Double)reader["I55XuatCondBHGiamDinh"];
			entity.I56TonCondBh = (System.Double)reader["I56TonCondBH"];
			entity.I57TonBupro = (System.Double)reader["I57TonBupro"];
			entity.I58TenMeMncs = (System.String)reader["I58TenMeMNCS"];
			entity.I59VaoDoz3002 = (System.Double)reader["I59VaoDOZ3002"];
			entity.I60RaDoz7900 = (System.Double)reader["I60RaDOZ7900"];
			entity.I61MeMncsSoLift = (System.String)reader["I61MeMNCSSoLift"];
			entity.I62XuatMeMncs = (System.Double)reader["I62XuatMeMNCS"];
			entity.I63TonCondNcsTaiKctv = (System.Double)reader["I63TonCondNCSTaiKCTV"];
			entity.I64XuatCondNcsTheoTank = (System.Double)reader["I64XuatCondNCSTheoTank"];
			entity.I65XuatCondNcsTheoGiamDinh = (System.Double)reader["I65XuatCondNCSTheoGiamDinh"];
			entity.I66SuKienVanHanh = (System.String)reader["I66SuKienVanHanh"];
			entity.I67NoteBcn = (System.String)reader["I67NoteBCN"];
			entity.I68NoteQlsplbh = (System.String)reader["I68NoteQLSPLBH"];
			entity.I69NoteCondBhMe = (System.String)reader["I69NoteCondBHMe"];
			entity.I70NoteGdme = (System.String)reader["I70NoteGDME"];
			entity.I71NoteMncs = (System.String)reader["I71NoteMNCS"];
			entity.Status = (System.Int32)reader["Status"];
			entity.InputUser = (System.Int32)reader["InputUser"];
			entity.Hid = reader.IsDBNull(reader.GetOrdinal("Hid")) ? null : (System.Int64?)reader["Hid"];
			entity.CreatedDate = (System.DateTime)reader["CreatedDate"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="SPL.Entities.Kvt"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="SPL.Entities.Kvt"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, SPL.Entities.Kvt entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.Id = (System.Int64)dataRow["Id"];
			entity.I1InputDate = (System.DateTime)dataRow["I1InputDate"];
			entity.I9KhiVaoDoGppNhap = (System.Double)dataRow["I9KhiVaoDoGPPNhap"];
			entity.I21XeBonFiqBupro = (System.Double)dataRow["I21XeBonFIQBupro"];
			entity.I34NhapCondBh = (System.Double)dataRow["I34NhapCondBH"];
			entity.I35MeBhSoKctv = (System.Double)dataRow["I35MeBHSoKCTV"];
			entity.I36NhapCondBhTuTk125 = (System.Double)dataRow["I36NhapCondBHTuTK125"];
			entity.I37NhapLpgTuTk107 = (System.Double)dataRow["I37NhapLPGTuTK107"];
			entity.I38Fqic1011 = (System.Double)dataRow["I38FQIC1011"];
			entity.I39Fqic1012 = (System.Double)dataRow["I39FQIC1012"];
			entity.I41XuatCondBhTheoMe = (System.Double)dataRow["I41XuatCondBHTheoMe"];
			entity.I42XuatCondBhLenhXuatSo = (System.String)dataRow["I42XuatCondBHLenhXuatSo"];
			entity.I43MeTdXuatBupro = (System.Double)dataRow["I43METdXuatBupro"];
			entity.I44TankXuatBupro = (System.Double)dataRow["I44TankXuatBupro"];
			entity.I45GiamDinhXuatBupro = (System.Double)dataRow["I45GiamDinhXuatBupro"];
			entity.I46XuatKhacLpg = (System.Double)dataRow["I46XuatKhacLPG"];
			entity.I47XuatKhacCondBh = (System.Double)dataRow["I47XuatKhacCondBH"];
			entity.I48XaDotLpg = (System.Double)dataRow["I48XaDotLPG"];
			entity.I49XaDotCondBh = (System.Double)dataRow["I49XaDotCondBH"];
			entity.I52XaDotKahxntlpg = (System.Double)dataRow["I52XaDotKAHXNTLPG"];
			entity.I53XaDotKahxntCond = (System.Double)dataRow["I53XaDotKAHXNTCond"];
			entity.I54XuatCondBhTank = (System.Double)dataRow["I54XuatCondBHTank"];
			entity.I55XuatCondBhGiamDinh = (System.Double)dataRow["I55XuatCondBHGiamDinh"];
			entity.I56TonCondBh = (System.Double)dataRow["I56TonCondBH"];
			entity.I57TonBupro = (System.Double)dataRow["I57TonBupro"];
			entity.I58TenMeMncs = (System.String)dataRow["I58TenMeMNCS"];
			entity.I59VaoDoz3002 = (System.Double)dataRow["I59VaoDOZ3002"];
			entity.I60RaDoz7900 = (System.Double)dataRow["I60RaDOZ7900"];
			entity.I61MeMncsSoLift = (System.String)dataRow["I61MeMNCSSoLift"];
			entity.I62XuatMeMncs = (System.Double)dataRow["I62XuatMeMNCS"];
			entity.I63TonCondNcsTaiKctv = (System.Double)dataRow["I63TonCondNCSTaiKCTV"];
			entity.I64XuatCondNcsTheoTank = (System.Double)dataRow["I64XuatCondNCSTheoTank"];
			entity.I65XuatCondNcsTheoGiamDinh = (System.Double)dataRow["I65XuatCondNCSTheoGiamDinh"];
			entity.I66SuKienVanHanh = (System.String)dataRow["I66SuKienVanHanh"];
			entity.I67NoteBcn = (System.String)dataRow["I67NoteBCN"];
			entity.I68NoteQlsplbh = (System.String)dataRow["I68NoteQLSPLBH"];
			entity.I69NoteCondBhMe = (System.String)dataRow["I69NoteCondBHMe"];
			entity.I70NoteGdme = (System.String)dataRow["I70NoteGDME"];
			entity.I71NoteMncs = (System.String)dataRow["I71NoteMNCS"];
			entity.Status = (System.Int32)dataRow["Status"];
			entity.InputUser = (System.Int32)dataRow["InputUser"];
			entity.Hid = Convert.IsDBNull(dataRow["Hid"]) ? null : (System.Int64?)dataRow["Hid"];
			entity.CreatedDate = (System.DateTime)dataRow["CreatedDate"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="SPL.Entities.Kvt"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">SPL.Entities.Kvt Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, SPL.Entities.Kvt entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region StatusSource	
			if (CanDeepLoad(entity, "Status|StatusSource", deepLoadType, innerList) 
				&& entity.StatusSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.Status;
				Status tmpEntity = EntityManager.LocateEntity<Status>(EntityLocator.ConstructKeyFromPkItems(typeof(Status), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.StatusSource = tmpEntity;
				else
					entity.StatusSource = DataRepository.StatusProvider.GetById(transactionManager, entity.Status);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'StatusSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.StatusSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.StatusProvider.DeepLoad(transactionManager, entity.StatusSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion StatusSource

			#region InputUserSource	
			if (CanDeepLoad(entity, "User|InputUserSource", deepLoadType, innerList) 
				&& entity.InputUserSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.InputUser;
				User tmpEntity = EntityManager.LocateEntity<User>(EntityLocator.ConstructKeyFromPkItems(typeof(User), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.InputUserSource = tmpEntity;
				else
					entity.InputUserSource = DataRepository.UserProvider.GetById(transactionManager, entity.InputUser);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'InputUserSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.InputUserSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.UserProvider.DeepLoad(transactionManager, entity.InputUserSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion InputUserSource

			#region HidSource	
			if (CanDeepLoad(entity, "Kvt|HidSource", deepLoadType, innerList) 
				&& entity.HidSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.Hid ?? (long)0);
				Kvt tmpEntity = EntityManager.LocateEntity<Kvt>(EntityLocator.ConstructKeyFromPkItems(typeof(Kvt), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.HidSource = tmpEntity;
				else
					entity.HidSource = DataRepository.KvtProvider.GetById(transactionManager, (entity.Hid ?? (long)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'HidSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.HidSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.KvtProvider.DeepLoad(transactionManager, entity.HidSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion HidSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetById methods when available
			
			#region KvtCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<Kvt>|KvtCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'KvtCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.KvtCollection = DataRepository.KvtProvider.GetByHid(transactionManager, entity.Id);

				if (deep && entity.KvtCollection.Count > 0)
				{
					deepHandles.Add("KvtCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<Kvt>) DataRepository.KvtProvider.DeepLoad,
						new object[] { transactionManager, entity.KvtCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the SPL.Entities.Kvt object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">SPL.Entities.Kvt instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">SPL.Entities.Kvt Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, SPL.Entities.Kvt entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region StatusSource
			if (CanDeepSave(entity, "Status|StatusSource", deepSaveType, innerList) 
				&& entity.StatusSource != null)
			{
				DataRepository.StatusProvider.Save(transactionManager, entity.StatusSource);
				entity.Status = entity.StatusSource.Id;
			}
			#endregion 
			
			#region InputUserSource
			if (CanDeepSave(entity, "User|InputUserSource", deepSaveType, innerList) 
				&& entity.InputUserSource != null)
			{
				DataRepository.UserProvider.Save(transactionManager, entity.InputUserSource);
				entity.InputUser = entity.InputUserSource.Id;
			}
			#endregion 
			
			#region HidSource
			if (CanDeepSave(entity, "Kvt|HidSource", deepSaveType, innerList) 
				&& entity.HidSource != null)
			{
				DataRepository.KvtProvider.Save(transactionManager, entity.HidSource);
				entity.Hid = entity.HidSource.Id;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();
	
			#region List<Kvt>
				if (CanDeepSave(entity.KvtCollection, "List<Kvt>|KvtCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(Kvt child in entity.KvtCollection)
					{
						if(child.HidSource != null)
							child.Hid = child.HidSource.Id;
						else
							child.Hid = entity.Id;

					}

					if (entity.KvtCollection.Count > 0 || entity.KvtCollection.DeletedItems.Count > 0)
					{
						DataRepository.KvtProvider.Save(transactionManager, entity.KvtCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< Kvt >) DataRepository.KvtProvider.DeepSave,
							new object[] { transactionManager, entity.KvtCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region KvtChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>SPL.Entities.Kvt</c>
	///</summary>
	public enum KvtChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Status</c> at StatusSource
		///</summary>
		[ChildEntityType(typeof(Status))]
		Status,
			
		///<summary>
		/// Composite Property for <c>User</c> at InputUserSource
		///</summary>
		[ChildEntityType(typeof(User))]
		User,
			
		///<summary>
		/// Composite Property for <c>Kvt</c> at HidSource
		///</summary>
		[ChildEntityType(typeof(Kvt))]
		Kvt,
	
		///<summary>
		/// Collection of <c>Kvt</c> as OneToMany for KvtCollection
		///</summary>
		[ChildEntityType(typeof(TList<Kvt>))]
		KvtCollection,
	}
	
	#endregion KvtChildEntityTypes
	
	#region KvtFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;KvtColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Kvt"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class KvtFilterBuilder : SqlFilterBuilder<KvtColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the KvtFilterBuilder class.
		/// </summary>
		public KvtFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the KvtFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public KvtFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the KvtFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public KvtFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion KvtFilterBuilder
	
	#region KvtParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;KvtColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Kvt"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class KvtParameterBuilder : ParameterizedSqlFilterBuilder<KvtColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the KvtParameterBuilder class.
		/// </summary>
		public KvtParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the KvtParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public KvtParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the KvtParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public KvtParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion KvtParameterBuilder
} // end namespace
